<?php

error_reporting(-1);
ini_set('display_errors', 1);

date_default_timezone_set('Asia/Ho_Chi_Minh');

class admin_forwarding extends CI_Controller {

    protected $_page_id = 'ul_postcontainer';
    protected $_page_name = "admin_forwarding";
    protected $data; //data for this page.
    protected $_page_title = ADMINTITLE_FORWARDING;
    protected $_base_url;
    protected $news_get_count;
    protected $returnCode;
    protected $news_lastest_url;
    protected $flag_check_lastest_news;
    protected $flag_check_update;

    function __construct() {
        $this->_initdata();
    }

    function index() {
        $this->load_knowledge_view();
        $this->showadmin($this->data);
    }

    function insert() {
        if (isset($_POST['isPost'])) {
            $title = $_POST['txt_title'];
            $url = convertToVn(trim($title));
            $content = $_POST['txt_content'];
            $cate = $_POST['cbo_cate'];
            $result = $this->Adminmodel->insert_forwarding_article($title, $url, $content, $cate);
            if ($result == 1) {
                echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' /><script type='text/javascript'>alert('Thêm bài viết thành công.');</script>";
                echo "<script>window.location.href='/admin_forwarding/insert'</script>";
            }
        } else {
            $this->data['page_name'] = "admin_forwarding_detail";
            $this->data['listCate'] = $this->getAllCateKnowledge();
            $this->data['title'] = "";
            $this->data['content'] = "";
            $this->data['selected_1'] = "";
            $this->data['selected_2'] = "";
            $this->data['selected_3'] = "";
            $this->data['pagePost'] = "insert";
            $this->showadmin($this->data);
        }
    }

    function edit() {
        $articleId = $this->uri->segment(3);

        if (isset($_POST['isPost'])) {
            $id = $articleId;
            $title = $_POST['txt_title'];
            $content = $_POST['txt_content'];
            $cate = $_POST['cbo_cate'];
            $order = $_POST['txt_order'];
            $result = $this->Adminmodel->update_forwarding_article($id, $title, $content, $cate, $order);
            if ($result == 1) {
                echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' /><script type='text/javascript'>alert('Chỉnh sửa bài viết thành công.');</script>";
                echo "<script>window.location.href='/admin_forwarding'</script>";
            }
        } else {
            $this->data['page_name'] = "admin_forwarding_detail";
            $this->data['listCate'] = $this->getAllCateKnowledge();
            $article = $this->Adminmodel->get_forwarding_article_by_id($articleId);
            $this->data['title'] = $article[0]->title;
            $this->data['content'] = $article[0]->content;
            $this->data['cateSelected'] = $article[0]->cate;
            $this->data['order'] = $article[0]->order;
            $this->data['pagePost'] = "edit/" . $articleId;
            $this->showadmin($this->data);
        }
    }

    function delete() {
        $id = $this->uri->segment(3);

        $result = $this->Adminmodel->delete_forwarding_article($id);
        if ($result == 1) {
            echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' /><script type='text/javascript'>alert('Xóa bài viết thành công.');</script>";
            echo "<script>window.location.href='/admin_forwarding'</script>";
        }
    }

    function load_knowledge_view() {
        $this->data['listCate'] = $this->getAllCateKnowledge();
        $this->load_articles_list();
    }

    function load_articles_list() {
        $param = $_SERVER['QUERY_STRING'] == "" ? "" : "?" . $_SERVER['QUERY_STRING'];
        $iCurPage = $this->uri->segment(2);
        if ($iCurPage < 1) {
            $iCurPage = 1;
        }

        @$limit = PAGESIZE;
        $start = $iCurPage * PAGESIZE - PAGESIZE;
        if (!($start > 0)) {
            $start = 0;
        }

        if (!$limit > 0) {
            $limit = 20;
        }

        $customs_name = isset($_GET['txt_title']) ? $_GET['txt_title'] : "";
        $customs_cate = isset($_GET['cbo_cate']) ? $_GET['cbo_cate'] : "";

        list($this->data['customsCount'], $this->data['aCustoms']) = $this->getKnowledgeArticles($customs_name, $customs_cate, $limit, $start);
        $iRow_Count = $this->data['customsCount'];
        $this->data['paging'] = $this->paging($iCurPage, ceil($iRow_Count / PAGESIZE), $this->config->item('base_url') . $this->_page_name, $iRow_Count, $param);
        $this->data['cur_page'] = $iCurPage;
        $this->data['previous_id'] = 1;
    }

    function getAllCateKnowledge() {
        $result = $this->Adminmodel->get_all_cate_forwarding();
        return $result;
    }

    function getKnowledgeArticles($title, $cate, $page_size, $page_index) {
        $result = $this->Adminmodel->get_forwarding_articles($title, $cate, $page_size, $page_index);
        return $result;
    }

    //local func
    function paging($page, $totalpage, $pagelink, $totalrows, $param) {
        $paging_str = '';
        $paging_str .= 'Trang: &nbsp;&nbsp;&nbsp;';
        if ($page > 1)
            $paging_str .= '<img src="' . $this->config->item('img_url') . 'style/img/img21_arrow_left.gif" align="absmiddle" style="cursor:pointer;" onclick="location.href=' . "'" . $pagelink . '/' . ($page - 1) . $param . "'" . '">';
        else
            $paging_str .= '<img src="' . $this->config->item('img_url') . 'style/img/img21_arrow_left.gif" align="absmiddle">';
        $paging_str .= '&nbsp;&nbsp;&nbsp;';

        // list 10 pagelink
        $startpage = 1;
        if ($page > 5)
            $startpage = $page - 4;
        $endpage = $totalpage;
        if ($totalpage - $page > 5)
            $endpage = $page + 5;
        for ($i = $startpage; $i < $page; $i++)
            $paging_str .= ' <a href="' . $pagelink . '/' . $i . $param . '" class="link">' . $i . '</a>';
        $paging_str .= ' <a href="' . $pagelink . '/' . $i . $param . '" class="link_selected">' . $page . '</a>';
        for ($i = $page + 1; $i <= $endpage; $i++)
            $paging_str .= ' <a href="' . $pagelink . '/' . $i . $param . '" class="link">' . $i . '</a>';

        $paging_str .= '&nbsp;&nbsp;&nbsp;';
        if ($page < $totalpage)
            $paging_str .= '<img src="' . $this->config->item('img_url') . 'style/img/img22_arrow_right.gif" align="absmiddle" style="cursor:pointer;" onclick="location.href=' . "'" . $pagelink . '/' . ($page + 1) . $param . "'" . '">';
        else
            $paging_str .= '<img src="' . $this->config->item('img_url') . 'style/img/img22_arrow_right.gif" align="absmiddle">';
        $paging_str .= '&nbsp;&nbsp;&nbsp;</span></td>';

        $paging_str .= '&nbsp;&nbsp;&nbsp;<span class="paging_total"><strong>[ </strong>T&#7893;ng c&#7897;ng: <font color="Red">' . $totalrows . '</font> k&#7871;t qu&#7843; <strong> ]</strong></span>';
        return $paging_str;
    }

    function _initdata() {
        parent::__construct();
        session_start();
        set_time_limit(0);
        $this->load->model('Adminmodel');
        $this->load->helper('url');
        $this->_check_admin_login();

        //init data (this page, header admin)

        $this->data['static_url'] = $this->config->item('img_url');
        $this->data['base_url'] = $this->config->item('base_url');
        $this->data['img_url'] = $this->config->item('img_url');
        $this->data['page_title'] = $this->_page_title;
        $this->data['admintitle_news'] = ADMINTITLE_NEWS;
        $this->data['admintitle_news_auto'] = ADMINTITLE_NEWS_AUTO;
        $this->data['admintitle_video'] = ADMINTITLE_VIDEO;
        $this->data['admintitle_customs'] = ADMINTITLE_CUSTOMS;
        $this->data['admintitle_forwarding'] = ADMINTITLE_FORWARDING;
        $this->data['admintitle_adv'] = ADMINTITLE_ADV;
        $this->data['admintitle_faq'] = ADMINTITLE_FAQ;
        $this->data['admintitle_contact'] = ADMINTITLE_CONTACT;
        $this->data['admintitle_common'] = ADMINTITLE_COMMON;
        $this->data['admintitle_stats'] = ADMINTITLE_STATS;
        $this->data['admintitle_beuser'] = ADMINTITLE_BEUSER;
        $this->data['website_name'] = WEBSITE_NAME;
        $this->data['link_page_name'] = $this->_page_name;
        $this->data['is_addnew'] = ""; //show link add
        $this->data['is_addlist'] = "NOT_SHOW"; //show link list
        $this->data['is_news_auto'] = ""; //show button get news auto
        $this->data['page_id'] = $this->_page_id;
        $this->data['page_name'] = $this->_page_name;
        $this->news_get_count = 0;
    }

    function _check_admin_login() {
        if (($this->Adminmodel->_check_login(SESSION_ADMINUSERNAME) == false) ||
                (time() - $this->Adminmodel->_get_session(SESSION_ADMINTIMEOUT)) > SESSION_ADMINTIMEOUTVALUE) {
            $base_url = $this->config->item('base_url');
            redirect($base_url . 'admin_login', 'location', 301);
        }
        else
            $this->Adminmodel->_set_session(SESSION_ADMINTIMEOUT, time());
    }

}

?>
